package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"server/internal/model"
)

func CreateCaseMeau(c *gin.Context) {
	_ = c.Request.ParseForm()
	meau := &model.CaseMeau{}

	_ = c.ShouldBind(meau)

	if len(meau.Name) == 0 {
		c.String(http.StatusOK, "目录名不能为空")
		return
	}
	if meau.ProjectID == 0 {
		c.String(http.StatusOK, "项目ID不能为空")
		return
	}
	if meau.FatherMD5 != nil && *meau.FatherMD5 != "" {
		meau.FatherMD5 = meau.FatherMD5
	}
	err := meau.Create()
	if err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": meau,
	})
}

func DeleteCaseMeau(c *gin.Context) {
	meau := &model.CaseMeau{}
	meau.Name = c.Param("name")
	if err := meau.Delete(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "目录删除成功")
}

func UpdateCaseMeau(c *gin.Context) {
	meau := &model.CaseMeau{}
	_ = c.ShouldBind(meau)
	if err := meau.Update(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "目录修改成功")
}

func QueryCaseMeau(c *gin.Context) {
	meau := &model.CaseMeau{}
	meau.Name = c.Param("name")
	if err := meau.Delete(); err != nil {
		c.String(http.StatusOK, err.Error())
		return
	}
	c.String(http.StatusOK, "目录删除成功")
}

func ListCaseMeau(c *gin.Context) {
	meau := &model.CaseMeau{}
	id := c.Query("id")
	if len(id) == 0 {
		id = "-1"
	}
	if id == "-1" {
		meau_list := meau.List()
		case_list := model.ListFatherNull()
		c.JSON(http.StatusOK, gin.H{
			"data":  meau_list,
			"cases": case_list,
		})
	} else {
		meau_list := meau.QueryById(id)
		case_list := model.QueryAllById(id)
		c.JSON(http.StatusOK, gin.H{
			"data":  meau_list,
			"cases": case_list,
		})
	}

}

func QueryCaseMeauInfo(c *gin.Context) {
	restful := &model.CaseMeau{}
	QueryByName(restful, c)
}
